﻿using AutoMapper;
using Entity.BaseInfo.DTO.Customer;
using Entity.BaseInfo.Entity;
using Entity.BaseInfo.View.Customer;
using Entity.Blog;
using Entity.Blog.Dto;
using Entity.Blog.Dto.BlogCategory;
using Entity.Blog.Dto.BlogLikes;
using FrameWork.DataContext;
using FrameWork.DataHelper;
using FrameWork.Request;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Service.Blog
{
    public class BlogSystemConfigService
    { 
        /**
         * 数据库
         */
        private readonly DataContext _context;

        /**
         * 自动映射
         */
        private readonly IMapper _mapper;

        /**
         * 构造注入
         */
        public BlogSystemConfigService(DataContext context, IMapper mapper)
        {
            _context = context;
            _mapper = mapper;
        }

        /**
         * 获取详情
         */
        public async Task<BlogSystemConfig> GetAsync()
        {
            return await _context.BlogSystemConfig.FirstOrDefaultAsync();
        }

        /**
         * 更新
         */
        public async Task<RequestResponseModel<string>> Put(BlogSystemConfigUpdateDto model)
        {
            //校验数据
            var query = await Valid.ValidId<BlogSystemConfig>(_context.BlogSystemConfig, model.Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", model.Id);
            }

            //编辑数据
            var entity = _mapper.Map(model, query);

            //更新表
            _context.BlogSystemConfig.Update(entity);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "更新成功", entity.Id);
        }
    }
}
